package com.csii.oauth.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class AuthExceptionHandler {
    @Autowired
    ExceptionOutput exceptionOutput;
	/**
	 * 处理自定义异常
	 */
	@ResponseBody
	@ExceptionHandler({AccessDeniedException.class, AuthenticationException.class})
	public ResponseEntity<String> handleAuthException(Exception e) {
		log.error(e.getMessage(), e);
		ResponseEntity entity= exceptionOutput.outputAuthException(e);
		entity.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
		return entity;
	}
}